<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="searchHandle">
      <el-row>
        <el-form-item label="销售单号">
          <el-input v-model="dataForm.billId"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <el-select v-model="dataForm.deptId" placeholder="请选择部门" clearable>
            <el-option v-for="item in deptOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="顾客姓名">
          <el-input v-model="dataForm.customerName"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="dataForm.customerPhone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
          <el-button icon="el-icon-delete" @click="searchReact">清空</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <div class="avue-crud">
      <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading">
        <el-table-column prop="billId" label="销售单号" width="170"></el-table-column>
        <el-table-column prop="customerName" label="顾客姓名" width="120"></el-table-column>
        <el-table-column prop="saleType" label="配镜类型" width="80">
          <template slot-scope="scope">
            {{getNameByValue(scope.row.saleType,saleTypeData)}}
          </template>
        </el-table-column>
        <el-table-column prop="saleStatus" label="销售状态" width="80">
          <template slot-scope="scope">
            {{getNameByValue(scope.row.saleStatus,saleStatusTypeData)}}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="销售日期" width="150"></el-table-column>
        <el-table-column prop="originalPrice" label="原价合计" width="80"></el-table-column>
        <el-table-column prop="receivablePrice" label="应收金额" width="80"></el-table-column>
        <el-table-column prop="discountPrice" label="折扣金额" width="80"></el-table-column>
        <el-table-column prop="removePrice" label="抹零金额" width="80"></el-table-column>
        <el-table-column prop="paidPrice" label="实缴金额" width="80"></el-table-column>
        <el-table-column label="操作" fixed="right" min-width="160">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="refundHandle(scope.row.billId)">处理</el-button>
            <el-button type="text" icon="el-icon-edit" @click="checkDetailByOrder(scope.row.billId)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>

    <order-info v-if="infoVisible" ref="infoDialog"></order-info>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { remote } from '@/api/admin/dict'
  import { fetchReturnMatchingPage,skipHisRefundSale } from '@/api/eims/cashier/refund'
  import { getDeptInfoByDeptType } from '@/api/admin/dept';
  import OrderInfo from '@/views/eims/optician/glassesinfo/detail'

  export default {
    data() {
      return {
        dataForm: {
          customerCard: '',
          billId: ''
        },
        deptOptions: [],
        dataList: [],
        saleTypeData: [],
        saleStatusTypeData: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        refundVisible: false,
        infoVisible: false
      }
    },
    created() {
      this.getDataList()
      remote('sale_type').then(result => {
        this.saleTypeData = result.data.data
      })
      remote('sale_status_type').then(result => {
        this.saleStatusTypeData = result.data.data
      })
      this.getDeptList()
    },
    computed: {
      ...mapGetters(['permissions', 'userInfo', 'bizData'])
    },
    components: {
      OrderInfo
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空搜索表单
      searchReact() {
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
      },
      //  获取数据列表
      getDataList() {
        this.dataListLoading = true
        if (this.userInfo.lessDeptId != 23) {
          this.dataForm.deptId = this.userInfo.lessDeptId
        }
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        let params2 = this.filterParams(this.dataForm)
        fetchReturnMatchingPage(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      //  每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      //  当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      //  退款处理
      refundHandle(billId) {
        this.$confirm('是否确认退款销售单', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return skipHisRefundSale({'billId': billId})
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('退款销售单处理成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  查看配镜单详情
      checkDetailByOrder(order) {
        this.infoVisible = true;
        this.$nextTick(() => {
          this.$refs.infoDialog.init('', order, '配镜单详情');
        });
      },
      //  根据value获取name值
      getNameByValue(value, data) {
        let obj = {}
        obj = data.find(item => {
          return item.value == value
        })
        if (obj) {
          return obj.label
        }
      },
      getDeptList() {
        getDeptInfoByDeptType(1).then(response => {
          if(response.data.code == '0'){
            let result = response.data.data;
            let list = [];
            result.forEach(item => {
              list.push({
                value: item.id,
                label: item.name
              });
            });
            this.deptOptions = list;
          }else{
            this.$message.error(response.data.msg)
          }
        });
      },
    }
  }
</script>
